package com.taiji.dicom4cloud.smart.alarm.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.taiji.dicom4cloud.common.core.constrant.CommonConstants;
import com.taiji.dicom4cloud.smart.alarm.annoation.HandlerType;
import com.taiji.dicom4cloud.smart.alarm.common.EnumHandler;
import com.taiji.dicom4cloud.smart.alarm.common.MessageHandler;
import com.taiji.dicom4cloud.smart.alarm.model.Message;
import com.taiji.dicom4cloud.smart.model.entity.AlarmEquipment;
import com.taiji.dicom4cloud.smart.model.entity.RfidRecord;
import com.taiji.dicom4cloud.smart.service.AlarmEquipmentService;
import com.taiji.dicom4cloud.smart.service.AlarmRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/7/21
 **/
@Slf4j
@HandlerType(value = EnumHandler.RESET)
public class ResetHandler implements MessageHandler {
    @Autowired
    private AlarmEquipmentService alarmEquipmentService;
    @Autowired
    private AlarmRecordService alarmRecordService;
    @Override
    public Object handle(Message message) {
        AlarmEquipment alarmEquipment = alarmEquipmentService.getByEquipmentId(message.getEquipmentId());
        if(ObjectUtil.isNotNull(alarmEquipment)){

            RfidRecord record = new RfidRecord();
            BeanUtil.copyProperties(alarmEquipment,record);
            record.setEquipmentName(alarmEquipment.getAlarmEquipmentName());
            BeanUtil.copyProperties(message,record);
            record.setCreateTime(LocalDateTime.now());
            record.setType(CommonConstants.EquipmentType.electricMeter);
            record.setAlarmName(message.getCode());
            alarmRecordService.saveRecord(record);
            log.info("设备复位消息:"+ JSONObject.toJSONString(message));

        }
        return "RESET";

    }
}